package com.neusoft.utils.json;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

/**
 * 转换json时 java.util.Date类型的数据做自定义处理
 * 
 * @author wang-zh.neu
 * 
 */
public class JsonDateProcessor implements JsonValueProcessor {

	private final static String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";
	
	private String format = "";

	public JsonDateProcessor() {
		this(DEFAULT_PATTERN);
	}

	public JsonDateProcessor(String pattern) {
		this.format = pattern;
	}

	@Override
	public Object processArrayValue(Object value, JsonConfig config) {
		return process(value);
	}

	@Override
	public Object processObjectValue(String key, Object value, JsonConfig config) {
		return process(value);
	}

	private Object process(Object value) {
		if (value instanceof Date) {
			SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINA);
			return sdf.format(value);
		}
		return value == null ? "" : value.toString();
	}
}
